package edu.stanford.prpl.common.util;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import edu.stanford.prpl.common.PRPLConstants;
import edu.stanford.prpl.common.PRPLProperties;

public class URLUtils {

	public static HttpURLConnection getHttpURLConnection(URL serverURL) throws IOException {

		String userKey = PRPLProperties.getProperty("prpl.user.key");
		String deviceName = PRPLProperties.getProperty("prpl.device.name");

		HttpURLConnection conn = (HttpURLConnection) serverURL.openConnection();
		conn.setRequestMethod("POST");
		conn.setReadTimeout(PRPLConstants.PRPL_X_REQUEST_DEFAULT_READ_TIMEOUT);
		conn.addRequestProperty(PRPLConstants.PRPL_X_REQUESTER_USER_URI, URIUtils.getIdentityURI(userKey));
		conn.addRequestProperty(PRPLConstants.PRPL_X_REQUESTER_DEVICE_URI, URIUtils.getDeviceURI(userKey,
				deviceName));
		conn.setDoOutput(true);
		conn.setChunkedStreamingMode(8092);
		return conn;

	}

	public static HttpURLConnection getHttpURLConnection(URL serverURL, String userKey, String deviceName)
			throws IOException {

		HttpURLConnection conn = (HttpURLConnection) serverURL.openConnection();
		conn.setRequestMethod("POST");
		conn.setReadTimeout(PRPLConstants.PRPL_X_REQUEST_DEFAULT_READ_TIMEOUT);
		conn.addRequestProperty(PRPLConstants.PRPL_X_REQUESTER_USER_URI, URIUtils.getIdentityURI(userKey));
		conn.addRequestProperty(PRPLConstants.PRPL_X_REQUESTER_DEVICE_URI, URIUtils.getDeviceURI(userKey,
				deviceName));
		conn.setDoOutput(true);
		conn.setChunkedStreamingMode(8092);
		return conn;

	}
}
